<?php

namespace common\entities\common;


use common\entities\ConfigEntity;
use Yii;

/**
 * This is the model class for table "myscrm_contract".
 *
 * @property string $ContractId
 * @property string $ContractNum
 * @property string $PartyA
 * @property integer $ContractType
 * @property string $BeginOn
 * @property string $EndOn
 * @property string $OrganizationId
 * @property integer $Status
 */
class MyscrmContractEntity extends ConfigEntity
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'myscrm_contract';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['ContractId', 'ContractNum', 'OrganizationId'], 'required'],
            [['ContractType', 'Status'], 'integer'],
            [['BeginOn', 'EndOn'], 'safe'],
            [['ContractId', 'OrganizationId'], 'string', 'max' => 36],
            [['ContractNum'], 'string', 'max' => 64],
            [['PartyA'], 'string', 'max' => 512]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'ContractId' => 'Contract ID',
            'ContractNum' => 'Contract Num',
            'PartyA' => 'Party A',
            'ContractType' => 'Contract Type',
            'BeginOn' => 'Begin On',
            'EndOn' => 'End On',
            'OrganizationId' => 'Organization ID',
            'Status' => 'Status',
        ];
    }
    
    
    /**
     * 根据合同编号，获取模型数据
     * @param string $contractNum 合同编号
     * @param string $orgId 租户Id
     * @return MyscrmContractEntity array 实体数据
     */
    public static function getContractByContractNum($contractNum, $orgId=null) {

        return self::find()->where(['ContractNum'=>$contractNum])
            ->andFilterWhere(['OrganizationId' => $orgId])
            ->one();
    }


    /**
     * 判断合同是否过期
     * @param $contract
     * @return bool
     */
    public static function isContractOverdue($contract){
        if(!empty($contract) && isset($contract['EndOn'])){
            if(strtotime($contract['EndOn']) >= time()){
                return false;
            }
        }

        return true;
    }
}
